package org.irri.smta.service.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.irri.smta.domain.ReportLog;
import org.irri.smta.repository.IReportLogRepository;
import org.irri.smta.service.IReportLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

@Named("reportLogService")
@Transactional(readOnly = true)
public class ReportLogService implements IReportLogService<ReportLog, Long> {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ReportLogService.class);

	@Inject
	@Named("reportLogRepository")
	private IReportLogRepository reportLogRepository;
	
	@Transactional(readOnly = true)
	@Override
	public Long getRowCount() {
		LOGGER.debug("Long getRowCount()");
		Long rowCount = reportLogRepository.count();
		LOGGER.debug("rowCount = " + rowCount);
		return rowCount;
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	@Override
	public void addReportLog(String message) {
		LOGGER.debug("addReportLog(String message): " + message);
		ReportLog reportLog = new ReportLog();
		reportLog.setMessage(message);
		create(reportLog);
	}
	
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	@Override
	public ReportLog create(ReportLog reportLog) {
		LOGGER.debug("ReportLog create(ReportLog reportLog): " + reportLog);
		Date now = new Date();
		reportLog.setCreatedDate(now);
		LOGGER.debug("Creating a new ReportLog with information: " + reportLog);
		return reportLogRepository.save(reportLog);
	}

	@Transactional(readOnly = true)
	@Override
	public ReportLog findById(Long id) {
		LOGGER.debug("ReportLog findById(Long id): " + id);
		return reportLogRepository.findOne(id);
	}

	@Transactional(readOnly = true)
	@Override
	public List<ReportLog> findAll() {
		LOGGER.debug("List<ReportLog> findAll()");
		return reportLogRepository.findAll();
	}

	@Transactional(readOnly = true)
	@Override
	public List<ReportLog> findAllOrdered() {
		LOGGER.debug("List<ReportLog> findAllOrdered()");
		return reportLogRepository.findAllOrdered();
	}

	@Transactional(readOnly = true)
	@Override
	public List<ReportLog> findAll(Pageable pageable) {
		LOGGER.debug("List<ReportLog> findAll(Pageable pageable)");
		Page<ReportLog> page = reportLogRepository.findAll(pageable);
		List<ReportLog> reportLogList = page.getContent();
		return reportLogList;
	}

	@Override
	public ReportLog update(ReportLog t) throws Exception {
		LOGGER.debug("ReportLog update(ReportLog t)");
		throw new UnsupportedOperationException("Update is unsupported!");
	}

	@Override
	public ReportLog delete(Long id) throws Exception {
		LOGGER.debug("ReportLog delete(Long id)");
		throw new UnsupportedOperationException("Delete is unsupported!");
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	@Override
	public void deleteReportLog() {
		LOGGER.debug("void truncateReportLog()");
		reportLogRepository.deleteReportLog();
	}

}
